import actionTypes from './types'
import api from '../api/index'
import io from 'socket.io-client'

export const authSuccess = (user) => ({type: actionTypes.AUTH_SUCCESS, data: user})
export const errorMsg = (msg) => ({type: actionTypes.ERROR_MSG, data: msg})
export const recrviteUser = (user) => ({type: actionTypes.RECEVITE_USER, data: user})
export const recrviteUserList = (users) => ({type: actionTypes.RECEVITE_USER_LIST, data: users})
export const resetUser = (msg) => ({type: actionTypes.RESET_USER, data: msg})
//消息类
export const receiveMsgList = ({users, chatMsgs ,userid}) => ({type:actionTypes.RECEVITE_MSG_LIST,data:{users, chatMsgs ,userid}})
export const receiveMsg = (chatMsg,userid) => ({type:actionTypes.RECEVITE_MAG,data:{chatMsg,userid}})
export const msgRead = ({from,to,count}) => ({type:actionTypes.MSG_READ,data:{from,to,count}})


export function regiter({username, password, password2, type}) {
    if (!username || !password || !type) {
        return errorMsg('请完善表格信息!')
    }
    if (password !== password2){
        return errorMsg('两次输入密码不一致!')
    }
    return async dispatch => {
        const res = await api.reqRegiter({username, password, type})
        const result = res.data
        if(result.code === 0){
            dispatch(authSuccess(result.data))
        }else {
            dispatch(errorMsg(result.message))
        }
    }
}

export function login({username, password}) {
    if (!username || !password ) {
        return errorMsg('请输入用户名或密码!')
    }
    return async dispatch => {
        const res = await api.reqLogin({username, password})
        const result = res.data
        if(result.code === 0){
            sessionStorage.setItem('userid',result.data._id)
            getMsgList(dispatch,result.data._id)
            dispatch(authSuccess(result.data))
        }else {
            dispatch(errorMsg(result.message))
        }
    }
}

export function update(user) {
    return async dispatch => {
        const res = await api.reqUpdateUser(user)
        const result = res.data
        if(result.code === 0){
            dispatch(authSuccess(result.data))
        }else {
            dispatch(errorMsg(result.message))
        }
    }
}

export function getUser(user_id) {
    return async dispatch => {
        const res = await api.reqUser(user_id)
        const result = res.data
        if(result.code === 0){
            sessionStorage.setItem('userid',result.data._id)
            getMsgList(dispatch, result.data._id)
            dispatch(recrviteUser(result.data))
        }else {
            dispatch(errorMsg(result.message))
        }
    }
}

export function getUserList(type) {
    return async dispatch => {
        const res = await api.reqUserList(type)
        const result = res.data;
        if(result.code === 0){
            dispatch(recrviteUserList(result.data))
        }else {
            dispatch(errorMsg(result.message))
        }
    }
}

// 消息类
export function initIo(dispath,userid) {
    if(!io.socket){
        io.socket = io('ws://47.97.212.129:3601')
        // io.socket = io('ws://localhost:4000')
        io.socket.on('receiveMsg', (chatMsg) =>{
            if(chatMsg.from === userid || chatMsg.to === userid){
                dispath(receiveMsg(chatMsg,userid))
            }
        })
    }
}

export async function getMsgList( dispath, userid) {
    initIo(dispath,userid)
    const  response  = await api.reqChatMsgList({userid:userid})
    const  result = response.data
    if(result.code === 0) {
        const {chatMsgs,users}  = result.data
        dispath(receiveMsgList({users, chatMsgs ,userid}))
    }
}
export  function sendMsg({from,to,content}) {
    return async dispath => {
       io.socket.emit('sendMsg',{from,to,content})
   }
}
export  function readMsg( from, to) {
    return async  (dispath) => {
        const  response  = await api.reqReadChatMsg(from,to)
        const  result = response.data
        if(result.code === 0) {
            const count  = result.data
            dispath(msgRead({ from, to,count}))
        }
    }
}
